package com.cpi.cornpp.transports.action;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.filter.BreadCrumbList;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.form.TransportForm;
import com.cpi.cornpp.transports.service.TransportService;
import com.cpi.cornpp.transports.view.CostElementView;
import com.cpi.cornpp.transports.view.TransportView;
import com.cs.common.init.beans.SiteMapDTO;

public class ShowAddTransportCostElement extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String SUCCESS_FORWARD = "toSuccess";//$NON-NLS-1$
	private static final String SHOW_NEW_TRANSPORT_FORWARD = "showNewTransport";
	private static final String SHOW_EDIT_TRANSPORT_FORWARD = "showEditTransport";
	private static final String COST_LIST_EMPTY = "error.message.transports.cost.list.empty";//$NON-NLS-1$
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse respose) throws TransportException{
		String messageError = null;
		TransportForm transportForm = null;
		TransportView transportView = null;
		TransportService transportService = null;
		List costElementsView = null;
		HttpSession session = null;
		ActionForward forward = null;
		MessageResources messageResources = null;
		
		try{
			
			session = request.getSession(false);
			messageResources = getResources(request);
			transportView = (TransportView) session.getAttribute("transportView");
			
			if ((form != null) && (transportView != null)){
				transportForm = (TransportForm) form;
				transportService = new TransportService();
				costElementsView = transportService.getCostElements();

				transportView.setTransportShortName(transportForm.getCode());
				transportView.setTransportDescription(transportForm.getDescription());
				transportView.setTransportCapacity((StringUtils.isNotEmpty(transportForm.getCapacity())) ? Double.parseDouble(transportForm.getCapacity()) : 0.0D);
				transportView.setTransportPriority((StringUtils.isNotEmpty(transportForm.getPriority())) ? Integer.parseInt(transportForm.getPriority()) : 0);
				transportView.setTransportNomenclature(transportForm.getNomenclature());
				
				removeAddedCosts(costElementsView,transportView.getTransportCostElements());
				
				if( costElementsView.size() == 0 ){
					session.setAttribute(ERROR_MESSAGE, messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), COST_LIST_EMPTY));
					forward = (StringUtils.isNotEmpty(transportForm.getId())) ? mapping.findForward(SHOW_EDIT_TRANSPORT_FORWARD) : mapping.findForward(SHOW_NEW_TRANSPORT_FORWARD);
				}else{
					session.setAttribute("costElementsList", costElementsView);
					session.setAttribute("transportView", transportView);
					request.setAttribute("costElementsList", costElementsView);
					request.setAttribute("backLink", (StringUtils.isNotEmpty(transportForm.getId())) ? "showEditTransport.do" : "showNewTransport.do");
					
					forward = mapping.findForward(SUCCESS_FORWARD);
				}
				
				//FIXME
				BreadCrumbList breadCrumbList = new BreadCrumbList(0);
				SiteMapDTO siteMapDTO = new SiteMapDTO();
				
				siteMapDTO.setLabel("label.breadcrum.home");
				siteMapDTO.setUrl("welcome.do");
				breadCrumbList.add(siteMapDTO);
				
				siteMapDTO = new SiteMapDTO();
				
				siteMapDTO.setLabel("label.breadcrum.transport.list");
				siteMapDTO.setUrl("showTransportQuery.do");
				breadCrumbList.add(siteMapDTO);
				
				siteMapDTO = new SiteMapDTO();
				
				siteMapDTO.setLabel((StringUtils.isNotEmpty(transportForm.getId())) ? "label.breadcrum.transport.edit" : "label.breadcrum.transport.new");
				siteMapDTO.setUrl((StringUtils.isNotEmpty(transportForm.getId())) ? "showEditTransport.do" : "showNewTransport.do");
				breadCrumbList.add(siteMapDTO);
				
				breadCrumbList.setLabel("label.breadcrum.costElement.add");
				
				session.setAttribute("BREADCRUMBS", breadCrumbList);
				session.setAttribute("BREADCRUMB_LABEL", breadCrumbList.getLabel());
			} else {
				forward = mapping.findForward("showTransportQuery");
			}

		} catch(TransportException tre){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), tre.getErrorCode());
			
			if(null == messageError){
				session.setAttribute(CUSTOM_EXCEPTION, tre);
				form.reset(mapping, request);
				throw tre;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = (StringUtils.isNotEmpty(transportForm.getId())) ? mapping.findForward(SHOW_EDIT_TRANSPORT_FORWARD) : mapping.findForward(SHOW_NEW_TRANSPORT_FORWARD);
			}
			
		} catch(Exception e){
			TransportException tre = new TransportException(e , TransportException.LAYER_ACTION, TransportException.ACTION_LISTS);;
			
			session.setAttribute(CUSTOM_EXCEPTION, tre);
			form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw tre;
		}
		
		return forward;
	}
	
	/**
	 * Remove costs added
	 * @param costElementsView
	 * @param costElementsAdded
	 */
	private void removeAddedCosts(List costElementsView, List costElementsAdded)throws TransportException{
		Iterator iteratorAdded = null;
		Iterator iteratorViews = null;
		CostElementView costElementViewAdded = null;
		CostElementView costElementView = null;
		int int_i = 0;
		try{
			if( costElementsAdded != null ){
				iteratorAdded = costElementsAdded.iterator();
				while(iteratorAdded.hasNext()){
					int_i = 0;
					costElementViewAdded = (CostElementView)iteratorAdded.next();
					iteratorViews = costElementsView.iterator();
					while( iteratorViews.hasNext() ){
						costElementView = (CostElementView)iteratorViews.next();
						if( costElementView.getChargeTypeShortName().equals(costElementViewAdded.getChargeTypeShortName())){
							costElementsView.remove(int_i);
							break;
						}
						int_i++;
					}
				}
			}
		}catch(Exception exception){
			TransportException tre = new TransportException(exception, TransportException.LAYER_ACTION, TransportException.ACTION_SELECT);
			throw tre;
		}
	}
}